/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_host_hdi.h"
#include "sle_hal_errcode.h"
#include "sle_hilog.h"
#include "sle_device_discovery.h"
#include "sle_common.h"
#include "sle_callback_adapter.h"

SleHdiErrorCode SleHdiAdapterEnableSle(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)enable_sle();
    return ret;
}

SleHdiErrorCode SleHdiAdapterDisableSle(void)
{
     SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
     ret = (SleHdiErrorCode)disable_sle();
     return ret;
}

SleHdiErrorCode SleHdiAdapterSetHostAddress(const SleDeviceAddress *addr)
{
    SLE_ASSERT_AND_RETURN_RET(addr!= NULL, SLE_HDI_ERRCODE_INVALID_PARAM);
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    sle_addr_t sle_addr = {
        .type = (uint8_t)addr->addrType,
    };
    memcpy(sle_addr.addr, addr->addr, sizeof(addr->addr));
    ret = (SleHdiErrorCode)sle_set_local_addr(&sle_addr);
    return ret;
}

SleHdiErrorCode SleHdiAdapterGetHostAddress(SleDeviceAddress *addr)
{
    SLE_ASSERT_AND_RETURN_RET(addr!= NULL, SLE_HDI_ERRCODE_INVALID_PARAM);
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    sle_addr_t sle_addr = {
        .type = (uint8_t)addr->addrType,
    };
    ret = (SleHdiErrorCode)sle_get_local_addr(&sle_addr);
    memcpy(addr->addr, sle_addr.addr, sizeof(addr->addr));
    return ret;
}

SleHdiErrorCode SleHdiAdapterSetHostName(const uint8_t *name, uint8_t len)
{
    SLE_ASSERT_AND_RETURN_RET(name != NULL, SLE_HDI_ERRCODE_INVALID_PARAM);
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_set_local_name(name, len);
    return ret;
}

SleHdiErrorCode SleHdiAdapterGetHostName(uint8_t *name, uint8_t *len)
{
    SLE_ASSERT_AND_RETURN_RET(name != NULL && len!= NULL, SLE_HDI_ERRCODE_INVALID_PARAM);
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = (SleHdiErrorCode)sle_get_local_name(name, len);
    return ret;
}

SleHdiErrorCode SleHdiAdapterRegisterHostCallback(SleHostHdiCallbacks *cbks)
{
    SLE_ASSERT_AND_RETURN_RET(cbks != NULL, SLE_HDI_ERRCODE_INVALID_PARAM);
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = RegisterHostCallbackAdapter(cbks);
    return ret;
}

SleHdiErrorCode GetSleHostHdiOpts(SleHostHdiOpts *hostOpts)
{
    SLE_ASSERT_AND_RETURN_RET_LOG(hostOpts != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "GetSleHostHdiOpts");
    hostOpts->enableSle = SleHdiAdapterEnableSle;
    hostOpts->disableSle = SleHdiAdapterDisableSle;
    hostOpts->setHostAddress = SleHdiAdapterSetHostAddress;
    hostOpts->getHostAddress = SleHdiAdapterGetHostAddress;
    hostOpts->setHostName = SleHdiAdapterSetHostName;
    hostOpts->getHostName = SleHdiAdapterGetHostName;
    hostOpts->registerHostCbks = SleHdiAdapterRegisterHostCallback;
    return SLE_HDI_ERRCODE_SUCC;
}